package cc.kigh.user.api;

import cc.kigh.common.annotation.RepeatSubmit;
import cc.kigh.common.constant.SessionConstants;
import cc.kigh.common.core.controller.BaseController;
import cc.kigh.common.core.domain.AjaxResult;
import cc.kigh.user.domain.bo.UmsUserBo;
import cc.kigh.user.domain.vo.ClientUserVo;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cc.kigh.user.service.IUmsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户Controller
 *
 * @author ruoyi
 * @date 2022-02-17
 */
@Validated
@Api(value = "前台用户控制器", tags = {"用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/api/ums/user")
public class ApiUmsUserController extends BaseController {

    private final IUmsUserService iUmsUserService;


    /**
     * 登录
     *
     * @return
     */
    @ApiOperation("登录")
    @RepeatSubmit()
    @PostMapping("/login")
    public AjaxResult<SaLoginModel> login(@RequestBody UmsUserBo bo) {

        return AjaxResult.success(iUmsUserService.login(bo));
    }

    /**
     * 登出
     *
     * @return
     */
    @ApiOperation("登出")
    @RepeatSubmit()
    @PostMapping("/logout")
    public AjaxResult<Void> logout() {

        return toAjax(iUmsUserService.logout());
    }

    /**
     * 注册
     *
     * @return
     */
    @ApiOperation("注册")
    @RepeatSubmit()
    @PostMapping("/registry")
    public AjaxResult<Void> registry(@RequestBody UmsUserBo bo) {

        return toAjax(iUmsUserService.registry(bo));
    }

    /**
     * 获取登录信息
     *
     * @return
     */
    @ApiOperation("获取登录信息")
    @GetMapping("/getUserInfo")
    @SaCheckLogin
    public AjaxResult<ClientUserVo> getUserInfo() {

        if (StpUtil.isLogin()) {
            return AjaxResult.success((ClientUserVo) StpUtil.getSession().get(SessionConstants.LOGIN_USER_INFO));
        }
        return AjaxResult.error("未登录", null);
    }

}
